package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeLineConsService;
import com.iknight.theory.model.TELineConsModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/cl")
@Api(value = "进户线控制器")
public class LineConsController implements BaseController<TELineConsModel> {
    private final static Logger logger = LogManager.getLogger(LineConsController.class);

    @Autowired
    private TeLineConsService<TELineConsModel, String> teLineConsService;

    @Override
    public TELineConsModel format(Map<String, Object> params) {
        TELineConsModel entity = new TELineConsModel();
        entity.setCl_id(IkgStringUtils.ObjectToString(params.get("cl_id")));
        if (IkgStringUtils.isNotNull(params.get("cl_no"))) {
            entity.setCl_no(IkgStringUtils.ObjectToString(params.get("cl_no")));
        }
        if (IkgStringUtils.isNotNull(params.get("cl_model"))) {
            entity.setCl_model(IkgStringUtils.ObjectToString(params.get("cl_model")));
        }
        if (IkgStringUtils.isNotNull(params.get("cl_length"))) {
            String value = IkgStringUtils.ObjectToString(params.get("cl_length"));
            entity.setCl_length(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("container_id"))) {
            String value = IkgStringUtils.ObjectToString(params.get("container_id"));
            entity.setContainer_id(value);
        }
        if (IkgStringUtils.isNotNull(params.get("start_pos"))) {
            String value = IkgStringUtils.ObjectToString(params.get("start_pos"));
            entity.setStart_pos(value);
        }
        if (IkgStringUtils.isNotNull(params.get("is_effective"))) {
            String value = IkgStringUtils.ObjectToString(params.get("is_effective"));
            entity.setIs_effective(value);
        }
        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teLineConsService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teLineConsService.delete(IkgStringUtils.ObjectToString(params.get("cl_id")));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("cl_id"));
            TELineConsModel entity = teLineConsService.selectOne(pKey);
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TELineConsModel> pageInfo = teLineConsService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/getConsLinesByParamInPage")
    @ApiOperation(value = "根据参数分页查询用户线", httpMethod = "POST")
    public Object getConsLinesByParamInPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        Map param = new HashMap();
        param.put("pagestart", pageIndex);
        param.put("pagesize", pageSize);
        if (null != params.get("tg_id")) {
            param.put("tg_id", params.get("tg_id"));
        }
        if (null != params.get("tran_id")) {
            param.put("tran_id", params.get("tran_id"));
        }

        PageInfo<Map> pageInfo = teLineConsService.getConsLinessByParam(param);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("cl_id"));
    }
}
